package com.xiaoke.liantongdemo.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.content.Context;

import com.xiaoke.liantongdemo.bean.BusinessInputs;
import com.xiaoke.liantongdemo.bean.Group;
import com.xiaoke.liantongdemo.constants.Constants;

public class XmlUtil {
	
	public static BusinessInputs getBusinessInputs(int id) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		BusinessInputsHandler handler = new BusinessInputsHandler();
		File file = new File(Constants.DATA_PATH + Constants.BUSINESS_XML_PATH + id + Constants.XML_SUFFIX);
		try {
			SAXParser parser = factory.newSAXParser();
			parser.parse(file, handler);
			return handler.getBusinessInputs();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Group getGroup(Context context, String name) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		GroupHandler handler = new GroupHandler();
		try {
			InputStream is = context
					.getResources()
					.getAssets()
					.open(Constants.GROUP_XML_PATH + name
							+ Constants.XML_SUFFIX);
			SAXParser parser=factory.newSAXParser();
			parser.parse(is, handler);
			return handler.getGroup();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
